package com.testDemo.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.test.CodeUtils;

public class TestUtils {

    /**
     * 根路径
     */
    private static String ROOT_PATH = "E:/ESS-AOP";

    /**
     * 接口规范的位置
     */
    public static String INTERFACE_EXCEL_PATH = ROOT_PATH + "/trunk/1研发项目管理/102设计文档/1023接口协议/10232业务能力协议/";
    /**
     * 生成的div的位置(项目在服务器的webRoot下)
     */
    public static String DIV_PATH = "E:/";

    /**
     * 获取bizkey的地址
     */
    public static String bizKeyExcel = ROOT_PATH + "/trunk/1研发项目管理/102设计文档/1023接口协议/10231业务流程梳理/业务流程梳理/";

    /**
     * appkey文件列表位置
     */
    public static String APPKEY_PATH = ROOT_PATH
            + "/Code/ESS-AOP/N3/ec-aop-server/src/main/resources/devconfig/ecaop/trades/app/";

    /**
     * 根据前台的地址选择,来确定调用的地址
     */
    public static Map urlMap = asMap("1", "http://132.35.81.217:8000/aop/test", "2",
            "http://132.35.88.104/aop/aopservlet", "3", "http://132.35.81.197:4000/aop/aopservlet");

    /**
     * 省份编码
     */
    private static String[] provinceCode = new String[] { "17", "34", "76", "87", "97", "19", "38", "51", "70", "88",
            "90", "18", "31", "59", "75", "81", "84", "11", "36", "86", "91", "98", "10", "13", "71", "85", "22", "30",
            "50", "74", "79", "83", "89" };
    private static String[] provinceName = new String[] { "山东", "江苏", "河南", "甘肃", "黑龙江", "山西", "福建", "广东", "青海", "宁夏",
            "吉林", "河北", "上海", "广西", "江西", "四川", "陕西", "北京", "浙江", "云南", "辽宁", "总部", "内蒙古", "天津", "湖北", "贵州", "澳门",
            "安徽", "海南", "湖南", "西藏", "重庆", "新疆" };

    public static final String[] CHECK_KEYS = new String[] { "节点名称", "父节点名称", "是否必选", "类型", "说明" };

    public static final String CHECK_KEY = "msg";

    public static boolean isEmpty(List list) {
        return null == list || 0 == list.size();
    }

    public static boolean isEmpty(Map map) {
        return null == map || map.isEmpty();
    }

    public static boolean isEmpty(Object o) {
        return null == o || "".equals(o);
    }

    public static boolean isEmpty(String[] strs) {
        return null == strs || 0 == strs.length;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getSysDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return format.format(new Date());
    }

    /**
     * 获取当前时间并格式化(yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String getSysDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    /**
     * 生成Map
     * @param objects
     * @return
     */
    public static Map asMap(Object... objects) {
        Map m = new HashMap(objects.length / 2 + 1);
        int i = 0;
        for (; i < objects.length - 1; i += 2) {
            m.put(objects[i], objects[i + 1]);
        }

        if (i < objects.length) {
            m.put(objects[i], null);
        }
        return m;
    }

    /**
     * 获取Excel路径下的所有Excel文件路径名
     * @param folder
     * @return
     */
    public static List<String> getExcelFilePath(String folder) {
        List<String> filePaths = new ArrayList<String>();
        boolean flag = true;
        if (!folder.endsWith(".xlsx")) {
            File file = new File(folder);
            File[] fileList = file.listFiles();
            for (File fs : fileList) {
                if (fs.isFile()) {
                    if (fs.getName().endsWith(".xlsx") && !fs.isHidden()) {
                        filePaths.add(folder + fs.getName());
                        flag = false;
                    }
                }
            }
        }
        else {
            flag = false;
            filePaths.add(folder);
        }
        if (flag)
            filePaths = null;

        return filePaths;
    }

    /**
     * 获取该接口规范sheet页的读取范围
     * @return
     * @throws Exception
     */
    public static String[] getReadRange(Sheet sheet) throws Exception {
        int rowStart = -1, rowEnd = -1, colStart = -1, colEnd = -1;
        boolean isOver = false;// 是否结束判断的标识
        for (Row row : sheet) {
            for (Cell cell : row) {
                String cellContent = "";
                if (!isEmpty(cell)) {
                    cell.setCellType(Cell.CELL_TYPE_STRING); // 设置单元格格式为String
                    cellContent = cell.getStringCellValue();
                    if (!isEmpty(cellContent)) {// 如果该行有值,则默认该行为请求报文规范的最后一行
                        rowEnd = row.getRowNum();
                    }
                }
                // 判断是否为读取范围的开始
                if (CHECK_KEYS[0].equals(cellContent)) {
                    rowStart = row.getRowNum();
                    colStart = cell.getColumnIndex();
                }
                // 在有读取列的开始值的前提下,判断结束值
                if (-1 != colStart && CHECK_KEYS[0].equals(cellContent)) {
                    colEnd = cell.getColumnIndex();
                }
                // 获取读取行的结束值,以读取到[返回报文] 前有值的最后一行,同时,读取结束
                if ("返回报文：".equals(cellContent) || CHECK_KEYS[0].equals(cellContent)) {
                    isOver = true;
                    break;
                }
            }
            if (isOver) {// 如果是内存循环结束的判断,则结束循环
                break;
            }
        }
        // 表明是判断未正确完成
        if (!isOver || -1 == rowStart || -1 == rowEnd || -1 == colStart || -1 == colEnd) {
            throw new Exception(CodeUtils.READ_RANGE_ERROR);
        }
        // 读取的行范围
        String readRow = rowStart + "," + rowEnd;
        // 读取的列范围
        String readCol = colStart + "," + colEnd;
        return new String[] { readRow, readCol };
    }

    /**
     * 省份编码和省份名称转换方法
     * @param changeKey
     * @param isGetName
     * @return
     * @throws Exception
     */
    public static String changeProvince(Object changeKey, boolean isGetName) throws Exception {
        String[] keyArray = provinceName;
        String[] valueArray = provinceCode;
        if (isGetName) {
            keyArray = provinceCode;
            valueArray = provinceName;
        }
        String key = String.valueOf(changeKey);
        for (int i = 0; i < keyArray.length; i++) {
            if (keyArray[i].equals(key)) {
                return valueArray[i];
            }
        }
        throw new Exception("根据传入的省份参数:" + changeKey + ",未获取到对应的转换参数");
    }

    /**
     * 将Clob类型转换为String
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static String ClobToString(Clob clob) throws SQLException, IOException {
        String reString = "";
        java.io.Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        is.close();
        br.close();
        return reString;
    }
}
